package com.speedricemanager.other.base;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.jbm.core.iface.IAcross;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.speedricemanager.interfaces.DialogBackListener;
import com.speedricemanager.interfaces.DialogSuccess;
import com.speedricemanager.other.config.Config;
import com.speedricemanager.other.server.KeepService;
import com.speedricemanager.other.server.ServerModel;
import com.speedricemanager.other.utils.TextUtils;
import com.vondear.rxtools.view.RxTitle;
import com.vondear.rxtools.view.RxToast;

import rx.functions.Action1;


public class BaseFgm extends Fragment implements Action1<IAcross>{
    protected  int type;
    protected Context mContext;
    private InputMethodManager imm;

    protected void hiddenPan(){
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    protected void startActivity(Class end){
        startActivity(new Intent(getActivity(),end));
    }

    protected void startActivityForResult(int requestNum,Class end){
        startActivityForResult(new Intent(getActivity(),end),requestNum);
    }
    public void setTitle(RxTitle mRxTitle, int resource, View.OnClickListener click){
        mRxTitle.setLeftFinish(getActivity());
        mRxTitle.setRightIconVisibility(true);
        mRxTitle.setRightIcon(resource);
        mRxTitle.setRightOnClickListener(click);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    private Toast toast;
    //普通消息提示
    public void messageAlert(String msg){
       if(!isHidden()){
           toast = RxToast.info(mContext, msg, Toast.LENGTH_SHORT, true);
           toast.setGravity(Gravity.CENTER, 0, 0);
           toast  .show();
       }

    }

    @Override
    public void onDestroy() {
        hiddenPan();
        super.onDestroy();
    }

    /**
     * 请求接口完成回调
     * @param iAcross
     * @param methodName
     */
    protected   void onComplete(IAcross iAcross, String methodName){};

    /**
     * 请求接口错误回调
     * @param iAcross
     * @param methodName
     */
    protected void onError(IAcross iAcross, String methodName) {

    }
    protected void onExe(String methodName) {

    }

    @Override
    public void call(IAcross iAcross) {
        onExe(iAcross.method);
        if(Config.SUCCESS == iAcross.m_code){
            onComplete(iAcross,iAcross.method);
        }else{
            if(iAcross.m_code==400){
                ServerModel.getInterface().Disconnect(true);
                mContext.startService(new Intent(mContext.getApplicationContext(), KeepService.class));
                Config.CONNECT=0;
            }
            if(iAcross.m_code==408){
                Config.CONNECT+=1;
            }
            if(iAcross.m_code==401){
                ServerModel.getInterface().login();
            }
            if("Logout".equals(iAcross.method)){
                return;
            }
            errorAlert(iAcross.m_info);
            onError(iAcross,iAcross.method);
        }

    }

    //错误消息提示
    public void errorAlert(String error){
        if(!isHidden()){
            toast = RxToast.error(mContext, error, Toast.LENGTH_SHORT, true);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast  .show();
        }

    }


    //带有输入框的dialog
    protected QMUIDialog alert;
    public void editAlert(String title, final DialogSuccess option){
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(mContext);
        alert = builder.setTitle(title)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        if(!TextUtils.isEmpty(builder.getEditText().getText().toString())){
                            messageAlert("请输入内容");
                            return ;
                        }
                        option.option(builder.getEditText().getText().toString());
                    }
                })
                .show();
    }

    protected void editDismiss(){
        if(alert!=null){
            alert.dismiss();
        }

    }

    public void showDeleteDialog(final DialogBackListener listener, final String... data){
        new QMUIDialog.MessageDialogBuilder(mContext)
                .setTitle(data[0])
                .setMessage(data[1])
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        listener.fails();
                        dialog.dismiss();

                    }
                })
                .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        //    Toast.makeText(BaseAty.this, "删除成功"+index, Toast.LENGTH_SHORT).show();
                        listener.success("");
                        dialog.dismiss();
                    }
                })
                .show();
    }

    //显示菜单
    protected void showMenuDialog(String[] menu, final DialogSuccess success){
        new QMUIDialog.MenuDialogBuilder(mContext)
                .addItems(menu, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, final int which) {
                        success.option(which+"");
                        dialog.dismiss();
                    }
                })
                .show();
    }

    //提示消息
    public void showAlert(final DialogSuccess listener, CharSequence comtent) {
        new QMUIDialog.MessageDialogBuilder(mContext)
                .setTitle("提示")
                .setMessage(comtent)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        if(listener!=null){
                            listener.option("");
                        }
                        dialog.dismiss();
                    }
                })
                .show();
    }
    private QMUITipDialog tipDialog;

}
